﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 链接列表类。
	/// </summary>
	public class LinkList : BaseRepeaterList
	{
		private LoadedValue<string> loadedClassCode;
		/// <summary>
		/// 列表的类别代码。
		/// </summary>
		public virtual string ClassCode
		{
			get
			{
				if (loadedClassCode.Loader == null)
					loadedClassCode.Loader = () => (string)ViewState["ClassCode"];
				return loadedClassCode.Value;
			}
			set
			{
				if (loadedClassCode.Setter == null)
					loadedClassCode.Setter = v => ViewState["ClassCode"] = v;
				loadedClassCode.Value = value;
				loadedLinkClassInfo.ResetValue();
			}
		}

		private LoadedValue<int?> loadedChannelId;
		public virtual int? ChannelId
		{
			get
			{
				if (loadedChannelId.Loader == null)
					loadedChannelId.Loader = () => (int?)ViewState["ChannelId"];
				return loadedChannelId.Value;
			}
			set
			{
				if (loadedChannelId.Setter == null)
					loadedChannelId.Setter = v => ViewState["ChannelId"] = v;
				loadedChannelId.Value = value;
			}
		}


		private LoadedValue<bool> loadedRandomSort;
		/// <summary>
		/// 是否随机排序。
		/// </summary>
		public virtual bool RandomSort
		{
			get
			{
				if (loadedRandomSort.Loader == null)
					loadedRandomSort.Loader = () => BooleanExtensions.ConvertFromOrDefault(ViewState["RandomSort"], false);
				return loadedRandomSort.Value;
			}
			set
			{
				if (loadedRandomSort.Setter == null)
					loadedRandomSort.Setter = v => ViewState["RandomSort"] = v;
				loadedRandomSort.Value = value;
			}
		}

		protected LoadedValue<LinkClass> loadedLinkClassInfo;
		/// <summary>
		/// 列表的模块代码。
		/// </summary>
		public LinkClass LinkClassInfo
		{
			get
			{
				if (loadedLinkClassInfo.Loader == null)
					loadedLinkClassInfo.Loader = () => SiteId.HasValue ? LinkClasses.GetItem(SiteId.Value, ClassCode) : null;
				return loadedLinkClassInfo.Value;
			}
		}




		private LoadedValue<string> loadedListTitle;
		/// <summary>
		/// 列表的标题。
		/// </summary>
		public override string ListTitle
		{
			get
			{
				if (loadedListTitle.Loader == null)
					loadedListTitle.Loader = () => (string)ViewState["ListTitle"];
				return string.IsNullOrEmpty(loadedListTitle.Value) ? LinkClasses.GetNameById(SiteId, ClassCode) : loadedListTitle.Value;
			}
			set
			{
				if (loadedListTitle.Setter == null)
					loadedListTitle.Setter = v => ViewState["ListTitle"] = v;
				loadedListTitle.Value = value;
			}
		}

		private LoadedValue<string> loadedListMoreUrl;
		/// <summary>
		/// 列表更多的Url地址。
		/// </summary>
		public override string ListMoreUrl
		{
			get
			{
				if (loadedListMoreUrl.Loader == null)
					loadedListMoreUrl.Loader = () => (string)ViewState["ListMoreUrl"];
				return string.IsNullOrEmpty(loadedListMoreUrl.Value) ? GenericUrlManager.BuildLinkListUrl(ClassCode, null) : loadedListMoreUrl.Value;
			}
			set
			{
				if (loadedListMoreUrl.Setter == null)
					loadedListMoreUrl.Setter = v => ViewState["ListMoreUrl"] = v;
				loadedListMoreUrl.Value = value;
			}
		}

		/// <summary>
		/// 列表的标题字段名。
		/// </summary>
		public override string TitleField
		{
			get { return "Title"; }
		}

		private LoadedValue<bool> loadedShowDefaultUrl;
		public virtual bool ShowDefaultUrl
		{
			get
			{
				if (loadedShowDefaultUrl.Loader == null)
					loadedShowDefaultUrl.Loader = () => BooleanExtensions.ParseOrDefault(Convert.ToString(ViewState["ShowDefaultUrl"]), true);
				return loadedShowDefaultUrl.Value;
			}
			set
			{
				if (loadedShowDefaultUrl.Setter == null)
					loadedShowDefaultUrl.Setter = v => ViewState["ShowDefaultUrl"] = v;
				loadedShowDefaultUrl.Value = value;
			}
		}

		/// <summary>
		/// 列表项的Url。
		/// </summary>
		public override string ItemUrl
		{
			get {
				if (ShowDefaultUrl)
				{
					return Eval("LinkUrl", null);
				}
				return string.Format(@"/Link_{0}.htm", Eval("LinkId", null)); 
			}
		}

		/// <summary>
		/// 列表的图片资源字段名。
		/// </summary>
		public override string ImageResourceField
		{
			get
			{
				return "ImageType";
			}
		}

		public virtual string ItemDescribe
		{
			get { return Convert.ToString(Eval("Describe")); }
		}


		

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>列表数据。</returns>
		public override object GetListData()
		{
			return Links.GetImageList(SiteId, ClassCode, ChannelId, RandomSort, LoadCount);
		}
	}
}